package com.BinarySheep.eTools;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LED extends Activity {

	// Handles
	private TextView mResults = null;
	private EditText mSourceV = null;
	private EditText mDiodeV = null;
	private EditText mDiodeC = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.led);
        
        // Get handles
		mResults = (TextView)findViewById(R.id.results);
		mSourceV = (EditText)findViewById(R.id.sourcev);
		mDiodeV = (EditText)findViewById(R.id.diodev);
		mDiodeC = (EditText)findViewById(R.id.diodec);
        
        // Hookup find resistor button
        Button button = (Button)findViewById(R.id.findr);
        button.setOnClickListener(findr_onClick);
	}
	
	OnClickListener findr_onClick = new OnClickListener() {
		public void onClick(View v) {
			// Parse user input			
			float sourceV = 0;
			float diodeV = 0;
			float diodeC = 0;		
			try {
				sourceV = Float.valueOf(mSourceV.getText().toString());
			} catch(NumberFormatException ex) { } 
			try {
				diodeV = Float.valueOf(mDiodeV.getText().toString());
			} catch(NumberFormatException ex) { } 
			try {
				diodeC = Float.valueOf(mDiodeC.getText().toString());
			} catch(NumberFormatException ex) { } 

			// Check inputs
			if (sourceV < diodeV) {
				mResults.setText("Error: Source voltage is larger than diode forward voltage");
				return;
			}
			
			// Calculate needed voltage drop			
			float vDrop = sourceV - diodeV;
			
			// Calculate resistor value for voltage drop
			// Vdrop (volts) / Idiode (mA) = resistor value (Ohms)
			float resistor = vDrop / (diodeC / 1000);
			
			// Convert resistor value to nice format
			DecimalFormat df = new DecimalFormat("###");
			String sResistor = df.format(resistor);
			
			// Calculate circuit wattage value
			// W = V * I = Vdrop * Idiode
			float watts =  vDrop * (diodeC / 1000);
			watts *= 2.0;
			df = new DecimalFormat("###.#");
			String sWatts = df.format(watts);
			
			// Create and show the results
			String sFinal = "Vsource = " + sourceV + "V\n";
			sFinal += "LED = " + diodeV + "V @ " + diodeC + "ma\n";
			sFinal += "use a " + sResistor + "\u2126 resistor\n";
			sFinal += "rated " + sWatts + "W or greater";
			mResults.setText(sFinal);
		}
	};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	boolean result = super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_led, menu);
    	return result;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.tools:
    		finish();
    		return true;
    	case R.id.about:
    		showAbout();
    		return true;
    	case R.id.help:
    		showHelp();
    		return true;
    	}
    	return super.onOptionsItemSelected(item);
    }

    protected void showHelp() {
        View messageView = getLayoutInflater().inflate(R.layout.help_led, null, false);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.help_led_title);
        builder.setView(messageView);
        builder.setPositiveButton("Done", null);
        builder.create();
        builder.show();
    }
    
    protected void showAbout() {
        // Inflate the about message contents
        View messageView = getLayoutInflater().inflate(R.layout.about, null, false);
        
        // When linking text, force to always use default color. This works
        // around a pressed color state bug.
        TextView textView = (TextView) messageView.findViewById(R.id.about_credits);
        int defaultColor = textView.getTextColors().getDefaultColor();
        textView.setTextColor(defaultColor);
        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        
        builder.setIcon(R.drawable.icon);
        builder.setTitle(R.string.app_name_short);
        builder.setView(messageView);
        builder.setPositiveButton("Done", null);
        builder.create();
        builder.show();
    }  
	
}

